/*#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"../include/main.h"
#include"../include/bst.h"
#include"../include/mount_vfs.h"
#include"../include/main_header.h"
#include"../include/narrtree.h"
#include"../include/hash.h"
#include"../include/constants.h"
#include"../include/vfs_errorcodes.h"
#include"../include/create_vfs.h"

FILE *vfs;

void mountvfs(char vfs_name[])
{
	size_t size;
	char ch[10];
	char *chh=ch;
	mainheader *header;
	llist *list;
	int i,flag,data_num_bak,len=0,var=0;
	
	data_num=0;
	glb_blk_num=1;
	head=NULL;
	vfs = fopen(vfs_name,"r+b");
	block_arr=(int *)malloc(sizeof(int)*size_of_vfs);
	if(size_of_vfs > 0 && size_of_vfs < 1025){
	for(i=0;i<size_of_vfs;i++)
		*(block_arr+i)=0;}
	if(vfs==NULL)
		printf("%s_FAILURE %s\n",glb_command,ERR_VFS_MOUNT_01);
	else
	{
		//printf("File opened for reading successfully.\n");
		header=(mainheader *)malloc(sizeof(mainheader));
		size=fread(&flag,INT_SIZE,1,vfs);
		if(size==0)
			printf("%s_FAILURE %s\n",glb_command,ERR_VFS_MOUNT_02);
		else if(flag==1)
		{
			mounted=1;
			printf("%s_FAILURE %s\n",glb_command,ERR_VFS_MOUNT_03);
		}
		else 
		{
			flag=1;
			size=fread(header->filelabel,MAX_NAME,1,vfs);
			size=fread(&header->max_fd,sizeof(header->max_fd),1,vfs);
			size=fread(&header->used_fd,sizeof(header->used_fd),1,vfs);
			local_max_fd = header->max_fd;
			local_used_fd = header->used_fd;
			//printf("local_max_fd=%d local_used_fd=%d\n",local_max_fd,local_used_fd);
			if(local_used_fd != 0 && local_used_fd < local_max_fd)
			{
				for(i=0;i<local_used_fd;i++)
				{
					header->fd_array[i]=(fdesc *)malloc(sizeof(fdesc));
					size=fread(header->fd_array[i]->filename,MAX_NAME,1,vfs);
					size=fread(header->fd_array[i]->filepath,MAX_PATH,1,vfs);
					size=fread(&header->fd_array[i]->filetype,INT_SIZE,1,vfs);
					size=fread(&header->fd_array[i]->file_size,INT_SIZE,1,vfs);
					size=fread(&header->fd_array[i]->blk_num,INT_SIZE,1,vfs);
				}
			
				for(i=0;i<local_used_fd;i++)
				{
					if(i==0)
						head=create(header->fd_array[0]);
					else
					{
						if(header->fd_array[i]->filetype == 0)
						{
							//while(header->fd_array[i]->filepath[len]!='\0')
							//	datafilepath[glb_blk_num][len]=header->fd_array[i]->filepath[len++];
							//datafilepath[glb_blk_num][len]='\0';
							*(block_arr+glb_blk_num)=header->fd_array[i]->blk_num;
							//block_arr++;
							glb_blk_num++;
						}
						head=insert_ntree(head,header->fd_array[i]);
					}
					list=insert(header->fd_array[i]);
					len=0;
				}			
				//printf("datafilepath[1]==%s   *(block_arr+1)==%d\n",datafilepath[1],*(block_arr+1));
			}
			i=0;
			fseek(vfs,MAX_NAME+(4*INT_SIZE)+(size_of_vfs*sizeof(fdesc)),SEEK_SET);
			size=fread(&flag,INT_SIZE,1,vfs);
			
			/*while(var < size_of_vfs-1)
			{
				size=fread(&flag,INT_SIZE,1,vfs);
				if(flag==1)
				{
					data_num++;
					size=fread(&flag,INT_SIZE,1,vfs);
				}
				var++;
			}*/
///////////////////////////////////
			/*if(flag==1)
			{
				do{
					data_num++;
					size=fread(&flag,INT_SIZE,1,vfs);	
				}while(flag!=0);
				
			}
			fseek(vfs,MAX_NAME+(3*INT_SIZE)+(size_of_vfs*sizeof(fdesc))+(size_of_vfs*INT_SIZE),SEEK_SET);
			data_num_bak=data_num;
			global_data_num_bak=data_num;
			//printf("data_num==%d\n",data_num);
			update_data(head,data_num_bak);
			/*while(data_num_bak > 0)
			{
				size=fread(data[i++],sizeof(DATA_SIZE),1,vfs);
				data_num_bak--;
			}*/
			//traverse(head);
///////////////////////////////
			/*printf("mountvfs_SUCCESS\n");
			mounted=1;
			flag=1;
			fseek(vfs,0,SEEK_SET);
			size=fwrite(&flag,INT_SIZE,1,vfs);
			fseek(vfs,0,SEEK_SET);
			size=fread(&flag,INT_SIZE,1,vfs);
			header=(mainheader *)malloc(sizeof(mainheader));
			size=fread(header->filelabel,MAX_NAME,1,vfs);
		}
	}
	if(vfs != NULL)
	close(vfs);
}
/*
void update_data(ntree *head,int data_num_bak)
{
	ntree *temp4,*q;
	q=head;
	size_t size;
	int toseek=MAX_NAME+(3*INT_SIZE)+(size_of_vfs*sizeof(fdesc))+(size_of_vfs*INT_SIZE);
	if(q!=NULL || data_num_bak > 0)
	{
		temp4=q;
		do
		{
			if(temp4->file_desc->filetype == 0 && data_num_bak > 0)			//while(data_num_bak > 0)
			{
				fseek(vfs,(toseek+(data_num_bak-1)*BLOCK_SIZE),SEEK_SET);
				size=fread(data[temp4->file_desc->blk_num],sizeof(DATA_SIZE),1,vfs);
				data_num_bak--;
			}
			update_data(temp4->first,data_num_bak);
			temp4=temp4->next;
		}while(temp4!=NULL);
		q=q->first;
	}
}*/


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"../include/main.h"
#include"../include/bst.h"
#include"../include/mount_vfs.h"
#include"../include/main_header.h"
#include"../include/narrtree.h"
#include"../include/hash.h"
#include"../include/constants.h"
#include"../include/vfs_errorcodes.h"
#include"../include/create_vfs.h"

FILE *vfs;

void mountvfs(char vfs_name[])
{
	size_t size;
	//char ch[10];
	//char *chh=ch;
	mainheader *header;
	llist *list;
	int i,flag,data_num_bak,len=0;
	
	data_num=0;
	glb_blk_num=1;
	//head=NULL;
	vfs = fopen(vfs_name,"r+b");
	block_arr=(int *)malloc(sizeof(int)*size_of_vfs);
	if(size_of_vfs > 0 && size_of_vfs < 1025){
	for(i=0;i<size_of_vfs;i++)
		*(block_arr+i)=0;}
	if(vfs==NULL)
		printf("%s_FAILURE %s\n",glb_command,ERR_VFS_MOUNT_01);
	else
	{
		//printf("File opened for reading successfully.\n");
		header=(mainheader *)malloc(sizeof(mainheader));
		size=fread(&flag,INT_SIZE,1,vfs);
		if(size==0)
			printf("%s_FAILURE %s\n",glb_command,ERR_VFS_MOUNT_02);
		else if(flag==1)
		{
			mounted=1;
			printf("%s_FAILURE %s\n",glb_command,ERR_VFS_MOUNT_03);
		}
		else 
		{
			head=NULL;
			flag=1;
			size=fread(header->filelabel,MAX_NAME,1,vfs);
			size=fread(&header->max_fd,sizeof(header->max_fd),1,vfs);
			size=fread(&header->used_fd,sizeof(header->used_fd),1,vfs);
			local_max_fd = header->max_fd;
			local_used_fd = header->used_fd;
			//printf("local_max_fd=%d local_used_fd=%d\n",local_max_fd,local_used_fd);
			if(local_used_fd != 0 && local_used_fd < local_max_fd)
			{
				for(i=0;i<local_used_fd;i++)
				{
					header->fd_array[i]=(fdesc *)malloc(sizeof(fdesc));
					size=fread(header->fd_array[i]->filename,MAX_NAME,1,vfs);
					size=fread(header->fd_array[i]->filepath,MAX_PATH,1,vfs);
					size=fread(&header->fd_array[i]->filetype,INT_SIZE,1,vfs);
					size=fread(&header->fd_array[i]->file_size,INT_SIZE,1,vfs);
					size=fread(&header->fd_array[i]->blk_num,INT_SIZE,1,vfs);
				}
			
				for(i=0;i<local_used_fd;i++)
				{
					if(i==0)
						head=create(header->fd_array[0]);
					else
					{
						//if(header->fd_array[i] != NULL)
						if(header->fd_array[i]->filetype == 0)
						{
							while(header->fd_array[i]->filepath[len]!='\0')
								datafilepath[glb_blk_num][len]=header->fd_array[i]->filepath[len++];
							datafilepath[glb_blk_num][len]='\0';
							*(block_arr+glb_blk_num)=header->fd_array[i]->blk_num;
							//block_arr++;
							glb_blk_num++;
						}
						head=insert_ntree(head,header->fd_array[i]);
					}
					list=insert(header->fd_array[i]);
					len=0;
				}			
				//printf("datafilepath[1]==%s   *(block_arr+1)==%d\n",datafilepath[1],*(block_arr+1));
			}
			i=0;
			fseek(vfs,MAX_NAME+(3*INT_SIZE)+(size_of_vfs*sizeof(fdesc)),SEEK_SET);
			size=fread(&flag,INT_SIZE,1,vfs);
			if(flag==1)
			{
				do{
					data_num++;
					size=fread(&flag,INT_SIZE,1,vfs);	
				}while(flag!=0);
			}
			fseek(vfs,MAX_NAME+(3*INT_SIZE)+(size_of_vfs*sizeof(fdesc))+(size_of_vfs*INT_SIZE),SEEK_SET);
			data_num_bak=data_num;
			global_data_num_bak=data_num;
			//printf("data_num==%d\n",data_num);
			update_data(head,data_num_bak);
			/*while(data_num_bak > 0)
			{
				size=fread(data[i++],sizeof(DATA_SIZE),1,vfs);
				data_num_bak--;
			}*/
			//traverse(head);
			printf("mountvfs_SUCCESS\n");
			mounted=1;
			flag=1;
			fseek(vfs,0,SEEK_SET);
			size=fwrite(&flag,INT_SIZE,1,vfs);
			fseek(vfs,0,SEEK_SET);
			size=fread(&flag,INT_SIZE,1,vfs);
			header=(mainheader *)malloc(sizeof(mainheader));
			size=fread(header->filelabel,MAX_NAME,1,vfs);
		}
	}
	if(vfs != NULL)
	close(vfs);
}

void update_data(ntree *head,int data_num_bak)
{
	ntree *temp4,*q;
	q=head;
	size_t size;
	int toseek=MAX_NAME+(3*INT_SIZE)+(size_of_vfs*sizeof(fdesc))+(size_of_vfs*INT_SIZE);
	if(q!=NULL)
	{
		temp4=q;
		do
		{
			if(temp4->file_desc->filetype == 0 && data_num_bak > 0)			//while(data_num_bak > 0)
			{
				fseek(vfs,(toseek+(data_num_bak-1)*BLOCK_SIZE),SEEK_SET);
				size=fread(data[temp4->file_desc->blk_num],sizeof(DATA_SIZE),1,vfs);
				data_num_bak--;
			}
			update_data(temp4->first,data_num_bak);
			temp4=temp4->next;
		}while(temp4!=NULL);
		q=q->first;
	}
}
